<?php
defined('IN_ZEEGING') or die('Access Denied');
require_once ZEEGING_PATH.'Librarys'.DIRECTORY_SEPARATOR.'ethereum'.DIRECTORY_SEPARATOR.'EthereumApi.php';
class RealMoneyDepositOrderModel extends Model{
	/*
	CREATE TABLE `deposit` (
	  `amount` float NOT NULL COMMENT '订单金额rmb',
	  `paymentmethod` varchar(50) NOT NULL COMMENT '支付方式',
	  `status` varchar(20) NOT NULL COMMENT '状态0 = inactive, 100 = failed, 200 = completed',
	  `username` int(100) NOT NULL COMMENT '用户唯一标识',
	  `createtime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '订单创建时间',
	  `updatetime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后更新时间',
	  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
	  PRIMARY KEY (`id`)
	) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='充值订单表'
	*/
	// INSERT INTO `deposit`(`amount`, `paymentmethod`, `status`, `username`, `createtime`, `updatetime`, `id`) VALUES ([value-1],[value-2],[value-3],[value-4],[value-5],[value-6],[value-7])
	// UPDATE `deposit` SET `amount`=[value-1],`paymentmethod`=[value-2],`status`=[value-3],`username`=[value-4],`createtime`=[value-5],`updatetime`=[value-6],`id`=[value-7] WHERE 1
	private $row;
	const ORDERSTATUS_INIT=0;
	const ORDERSTATUS_SUCCESS=200;
	const ORDERSTATUS_ERROR=100;

	public function __construct(){
		$this->row=(object)array();
		parent::__construct();
	}
	public function load($orderid){
		$sql="select * from `deposit` where id=$orderid";
		$res=$this->query($sql);
		if($res && $row=$this->fetch($res,'object')){
			$this->row=$row;
			return $this;
		}
		return false;	//这里应该抛出错误
	}
	public function save(){
		$amount=$this->row->amount;
		$paymentmethod=$this->row->paymentmethod;
		$status=$this->row->status;
		$username=$this->row->username;
		$insertSql="INSERT INTO `deposit`(`amount`, `paymentmethod`, `status`, `username`)
			VALUES ($amount, '$paymentmethod', $status, '$username')";
		$id=($this->execute($insertSql) ? $this->fetch($this->query('select @@IDENTITY;')) : null );
		if($id>0){
			$this->row->id=$id[0];
			return true;
		}
		else{
			return false;
		}
	}
	public function success(){
		if(!isset($this->row->id))return false;
		$id=$this->row->id;
		$updateSql="UPDATE `deposit` SET `status`=200 WHERE `id`=$id";
		return $res=$this->execute($updateSql)? true : false;
	}

	public function __get($name){
		return isset($this->row->$name)? $this->row->$name : false;
	}
	public function __set($name,$value){
		return $this->row->$name=$value;
	}
}